package cn.wen233.sunglow.controllers;

import cn.wen233.basic.http.RestResponse;
import cn.wen233.core.ServiceManager;
import cn.wen233.core.filter.InstanceFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Instance rest api
 *
 * @author wenei
 * @date 2021-10-24 15:31
 */
@RestController
@RequestMapping("/instances")
@RequiredArgsConstructor
public class InstanceApi {

    private final ServiceManager serviceManager;

    @PostMapping("/page")
    public RestResponse<?> page(@RequestBody InstanceFilter filter) {
        return RestResponse.success(serviceManager.findPageInstance(filter));
    }

    @GetMapping
    public RestResponse<?> getAll(String namespaceId, String serviceId) {
        return RestResponse.success(serviceManager.getAllInstance(namespaceId, serviceId));
    }

    @PostMapping
    public RestResponse<?> register(String namespaceId, String serviceId, String host, int port) {
        return RestResponse.success(serviceManager.registerInstance(namespaceId, serviceId, host, port));
    }

    @GetMapping("heartbeat")
    public RestResponse<?> heartbeat(String instanceId) {
        serviceManager.instanceHeartbeat(instanceId);
        return RestResponse.success();
    }

    @DeleteMapping
    public RestResponse<?> remove(String namespaceId, String serviceId, String id) {
        serviceManager.removeInstance(namespaceId, serviceId, id);
        return RestResponse.success();
    }
}
